package prefetch

import (
	"bytes"
	"encoding/json"
	"log"
	"net/http"
)

type Scheduler struct {
	daemonURL string
}

func NewScheduler(url string) *Scheduler {
	return &Scheduler{daemonURL: url}
}

func (s *Scheduler) Prefetch(blocks []string) error {
	if len(blocks) == 0 {
		return nil
	}
	body, _ := json.Marshal(map[string]interface{}{
		"blocks": blocks,
	})
	resp, err := http.Post(s.daemonURL+"/prefetch", "application/json", bytes.NewReader(body))
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	log.Printf("[scheduler] prefetch %d blocks", len(blocks))
	return nil
}
